package com.chixing.service.impl;

import com.chixing.mapper.CarMapper;
import com.chixing.mapper.MyorderMapper;
import com.chixing.pojo.*;
import com.chixing.service.MyorderService;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MyorderServiceImpl implements MyorderService {

    @Autowired
    private MyorderMapper myorderMapper;
    @Autowired
    private CarMapper carMapper;

    @Override
    public ServerResponse getByCar(String carModel) {
        CarExample carExample = new CarExample();
        carExample.createCriteria().andCarModelEqualTo(carModel);

        MyorderExample myorderExample = new MyorderExample();
        PageHelper.startPage(1,5);
        List<Myorder> myorderList = myorderMapper.selectByExample(myorderExample);

        List<OrderCarVO> orderCarVoList = new ArrayList<>();
        for (Myorder myorder:myorderList){
            Car car1 = carMapper.selectByPrimaryKey(myorder.getCarId());
            OrderCarVO orderCarVO = new OrderCarVO(myorder,car1);
            orderCarVoList.add(orderCarVO);
        }
        return ServerResponse.getSucccess(orderCarVoList);
    }


    @Override
    public ServerResponse getByStatus(Integer custId, Integer status,Integer pageNum) {
        MyorderExample myorderExample = new MyorderExample();
        MyorderExample.Criteria orderCriteria = myorderExample.createCriteria();

        orderCriteria.andCustIdEqualTo(custId);
        if (status !=null&&status!=-1){
            orderCriteria.andStatusEqualTo(status);
        }
        List<OrderCarVO> voList = new ArrayList<>();
        Page page = PageHelper.startPage(pageNum,5);
//        PageHelper.startPage(pageNum,5);
        List<Myorder> myorderList = myorderMapper.selectByExample(myorderExample);
        for (Myorder myorder:myorderList){
            OrderCarVO orderCarVO = new OrderCarVO();
            orderCarVO.setMyorder(myorder);

            Car car = carMapper.selectByPrimaryKey(myorder.getCarId());
            orderCarVO.setCar(car);

            voList.add(orderCarVO);
        }
        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(voList);
//        PageInfo<Myorder> pageInfo = new PageInfo<>(myorderList);
        if (myorderList!=null&&myorderList.size()>0){
            return ServerResponse.getSucccess(pageInfo);
        }else {
            return ServerResponse.getFail(null);
        }
    }


}

